/**
 * Copyright (c) 2017-2017 All Rights Reserved.
 */
package com.zbest.zpay.exchange.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zbest.common.lang.enums.BizCode;
import com.zbest.common.lang.exception.ZpayException;
import com.zbest.zpay.service.exchange.FinCardPayRequest;
import com.zbest.zpay.service.exchange.FinCardPayResponse;
import com.zbest.zpay.service.exchange.FinCardPayService;
import com.zbest.zpay.service.exchange.model.FluxKeyEnum;
import com.zbest.zpay.shared.process.ProcessResult;
import com.zbest.zpay.shared.process.ProcessService;

/**
 * 
 *
 * @author Administrator
 * @version $Id: FinAccountPayServiceImpl.java, v 0.1 2017-4-24 下午11:58:59
 *          Administrator Exp $
 */
@Service("finCardPayService")
public class FinCardPayServiceImpl implements FinCardPayService {

    private static final Logger logger = LoggerFactory.getLogger(FinCardPayServiceImpl.class);

    @Resource
    private ProcessService      processService;

    /** 
     * @see com.zbest.zpay.exchange.facade.FinAccountPayFacade#exchange(java.lang.String, java.util.Map)
     */
    @Override
    public FinCardPayResponse exchange(FinCardPayRequest finAccountPayRequest) {

        Map<String, Object> data = new HashMap<String, Object>();
        data.put(FluxKeyEnum.FIN_CARD_PAY_REQUEST.getCode(), finAccountPayRequest);
        return process(finAccountPayRequest.getChannelId(), data);
    }

    /**
     * 
     * @param processName
     * @param data
     * @return
     */
    private FinCardPayResponse process(String processName, Map<String, Object> data) {
        logger.info("使用账户支付channelId=" + processName + ",data=" + data);

        FinCardPayResponse response = new FinCardPayResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.putAll(data);
            ProcessResult result = processService.execute(processName, dataMap);
            response.setCode(result.getCode());
            response.getResultData().putAll(result.getResultObject());
        } catch (ZpayException e) {
            logger.error("账户支付处理异常data=" + data, e);
            response.setCode(e.getCode());
        } catch (Exception e) {
            logger.error("账户支付处理异常data=" + data, e);
            response.setCode(BizCode.SYSTEM_EXCEPTION);
        }

        logger.info("支付响应：" + response);
        return response;
    }

    /** 
     * @see com.zbest.zpay.exchange.facade.FinAccountPayFacade#instNotify(java.lang.String, java.util.Map)
     */
    @Override
    public FinCardPayResponse instNotify(String channelId, Map<String, Object> data) {
        return process("instNotify", data);
    }

}
